home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / System7 tools / Frontier / Frontier SDK 2.1 / Toolkits / Applet Toolkit / appletstrings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-11  |  7.2 KB  |  402 lines  |  [TEXT/KAHL]

  1.  
  2. /*© Copyright 1988-1992 UserLand Software, Inc.  All Rights Reserved.*/
  3.  
  4.  
  5. #include "appletops.h"
  6. #include "appletmemory.h"
  7. #include "appletstrings.h"
  8.  
  9.  
  10. boolean equalstrings (bs1, bs2) bigstring bs1, bs2; {
  11.  
  12.     /*
  13.     return true if the two strings (pascal type, with length-byte) are
  14.     equal.  return false otherwise.
  15.     */
  16.  
  17.     register ptrchar p1 = (ptrchar) bs1, p2 = (ptrchar) bs2;
  18.     register char ct = *p1 + 1;
  19.     
  20.     while (ct--) 
  21.         
  22.         if (*p1++ != *p2++)
  23.         
  24.             return (false);
  25.         
  26.     return (true); /*loop terminated*/
  27.     } /*equalstrings*/
  28.     
  29.     
  30. boolean unicaseequalstrings (bs1, bs2) bigstring bs1, bs2; {
  31.     
  32.     bigstring bscopy1, bscopy2;
  33.     
  34.     copystring (bs1, bscopy1);
  35.     
  36.     copystring (bs2, bscopy2);
  37.     
  38.     alllower (bscopy1);
  39.     
  40.     alllower (bscopy2);
  41.     
  42.     return (equalstrings (bscopy1, bscopy2));
  43.     } /*unicaseequalstrings*/
  44.     
  45.  
  46. void copystring (bssource, bsdest) void *bssource, *bsdest; {
  47.  
  48.     /*
  49.     create a copy of bssource in bsdest.  copy the length byte and
  50.     all the characters in the source string.
  51.  
  52.     assume the strings are pascal strings, with the length byte in
  53.     the first character of the string.
  54.     */
  55.  
  56.     register short i, len;
  57.     
  58.     len = (short) ((char *) bssource) [0];
  59.     
  60.     for (i = 0; i <= len; i++) 
  61.         ((char *) bsdest) [i] = ((char *) bssource) [i];
  62.     } /*copystring*/
  63.  
  64.  
  65. boolean pushstring (bssource, bsdest) bigstring bssource, bsdest; {
  66.  
  67.     /*
  68.     insert the source string at the end of the destination string.
  69.     
  70.     assume the strings are pascal strings, with the length byte in
  71.     the first character of the string.
  72.     */
  73.     
  74.     register short lensource = stringlength (bssource);
  75.     register short lendest = stringlength (bsdest);
  76.     register char *psource, *pdest;
  77.     
  78.     if ((lensource + lendest) > lenbigstring)
  79.         return (false);
  80.         
  81.     pdest = (ptrchar) bsdest + (char) lendest + 1;
  82.     
  83.     psource = (ptrchar) bssource + 1;
  84.     
  85.     bsdest [0] += (char) lensource;
  86.     
  87.     while (lensource--) *pdest++ = *psource++;
  88.     
  89.     return (true);
  90.     } /*pushstring*/
  91.     
  92.     
  93. boolean pushspace (bs) bigstring bs; {
  94.     
  95.     bigstring bsspace;
  96.     
  97.     setstringlength (bsspace, 1);
  98.     
  99.     bsspace [1] = ' ';
  100.     
  101.     return (pushstring (bsspace, bs));
  102.     } /*pushspace*/
  103.     
  104.  
  105. void pushlong (num, bsdest) long num; bigstring bsdest; {
  106.  
  107.     bigstring bsint;
  108.     
  109.     NumToString (num, bsint);
  110.     
  111.     pushstring (bsint, bsdest);
  112.     } /*pushlong*/
  113.     
  114.  
  115. void pushint (num, bsdest) short num; bigstring bsdest; {
  116.     
  117.     pushlong ((long) num, bsdest);
  118.     } /*pushint*/
  119.     
  120.     
  121. void allupper (bs) bigstring bs; {
  122.     
  123.     register char len = bs [0];
  124.     register ptrchar p = (ptrchar) &bs [1];
  125.     register char ch;
  126.     
  127.     while (len--) {
  128.         
  129.         ch = *p;
  130.         
  131.         if ((ch >= 'a') && (ch <= 'z'))
  132.             *p -= 32;
  133.             
  134.         p++;
  135.         } /*while*/
  136.     } /*allupper*/
  137.     
  138.     
  139. void alllower (bs) bigstring bs; {
  140.     
  141.     register char len = bs [0];
  142.     register ptrchar p = (ptrchar) &bs [1];
  143.     register char ch;
  144.     
  145.     while (len--) {
  146.         
  147.         ch = *p;
  148.         
  149.         if ((ch >= 'A') && (ch <= 'Z'))
  150.             *p += 32;
  151.             
  152.         p++;
  153.         } /*while*/
  154.     } /*alllower*/
  155.     
  156.     
  157. boolean stringlessthan (bs1, bs2) bigstring bs1, bs2; {
  158.     
  159.     register short i, ctloops;
  160.     register char ch1, ch2;
  161.     short len1, len2;
  162.     
  163.     len1 = (short) bs1 [0];
  164.     
  165.     len2 = (short) bs2 [0];
  166.     
  167.     ctloops = minint (len1, len2);
  168.     
  169.     for (i = 1; i <= ctloops; i++) {
  170.         
  171.         ch1 = bs1 [i];
  172.         
  173.         ch2 = bs2 [i];
  174.         
  175.         if (ch1 != ch2) /*we have our answer*/
  176.             return (ch1 < ch2);
  177.         } /*for*/
  178.     
  179.     return (len1 < len2); /*loop terminated, strings are equal up to the min length*/
  180.     } /*stringlessthan*/
  181.     
  182.     
  183. void midstring (bssource, ix, len, bsdest) bigstring bssource, bsdest; short ix, len; {
  184.     
  185.     setstringlength (bsdest, len);
  186.     
  187.     moveleft (bssource + ix, bsdest + 1, (long) len);
  188.     } /*midstring*/
  189.     
  190.     
  191. boolean pushchar (ch, bs) byte ch; bigstring bs; {
  192.     
  193.     /*
  194.     insert the character at the end of a pascal string.
  195.     */
  196.     
  197.     register short len;
  198.     
  199.     len = bs [0]; 
  200.     
  201.     if (len >= lenbigstring)
  202.         return (false);
  203.     
  204.     bs [++len] = ch;
  205.     
  206.     bs [0] = len;
  207.     
  208.     return (true);
  209.     } /*pushchar*/
  210.     
  211.     
  212. boolean scanstring (ch, bs, ix) byte ch; bigstring bs; short *ix; {
  213.     
  214.     /*
  215.     return in ix the index in the string of the first occurence of chscan.
  216.     
  217.     return false if it wasn't found, true otherwise.
  218.     
  219.     dmb 10/26/90: p is now initialized correctly to bs + i, not bs + 1
  220.     */
  221.     
  222.     register short i;
  223.     register ptrbyte p;
  224.     register byte c = ch;
  225.     register short len = stringlength (bs);
  226.     
  227.     for (i = *ix, p = bs + i; i <= len; i++) 
  228.         
  229.         if (*p++ == c) {
  230.             
  231.             *ix = i;
  232.             
  233.             return (true);
  234.             }
  235.             
  236.     return (false);
  237.     } /*scanstring*/
  238.  
  239.  
  240. boolean deletestring (bs, ixdelete, ctdelete) bigstring bs; short ixdelete, ctdelete; {
  241.     
  242.     /*
  243.     delete ct chars in the indicated string, starting with the character
  244.     at offset ix.
  245.     */
  246.     
  247.     register short ix = ixdelete;
  248.     register short ct = ctdelete;
  249.     register short len = stringlength (bs);
  250.     register long ctmove;
  251.     register ptrbyte pfrom, pto;        
  252.     
  253.     if ((ix > len) || (ix < 1))
  254.         return (false);
  255.         
  256.     if (ct <= 0)
  257.         return (ct == 0);
  258.         
  259.     ctmove = len - ix - ct + 1;
  260.      
  261.     if (ctmove > 0) {
  262.         
  263.         pfrom = bs + ix + ct;
  264.         
  265.         pto = bs + ix;
  266.         
  267.         moveleft (pfrom, pto, ctmove);
  268.         }
  269.     
  270.     setstringlength (bs, len - ct);
  271.     
  272.     return (true);
  273.     } /*deletestring*/
  274.     
  275.     
  276. boolean firstword (bssource, chdelim, bsdest) bigstring bssource, bsdest; char chdelim; {
  277.     
  278.     /*
  279.     copy the first word from bs, and put it into bsdest.
  280.     
  281.     search forwards from the beginning of the source string until you 
  282.     find chdelim.
  283.     */
  284.     
  285.     register short len = stringlength (bssource);
  286.     register short i;
  287.     register char ch = chdelim;
  288.     
  289.     for (i = 1; i <= len; i++) {
  290.         
  291.         if (bssource [i] == ch) {
  292.             
  293.             midstring (bssource, 1, i - 1, bsdest);
  294.             
  295.             return (true);
  296.             }
  297.         } /*for*/
  298.         
  299.     copystring (bssource, bsdest);
  300.     
  301.     return (true);
  302.     } /*firstword*/
  303.  
  304.  
  305. boolean lastword (bssource, chdelim, bsdest) bigstring bssource, bsdest; char chdelim; {
  306.     
  307.     /*
  308.     copy the last word from bs, and put it into bsdest.
  309.     
  310.     search backwards from the end of the source string until you find
  311.     chdelim.
  312.     */
  313.     
  314.     register short len = stringlength (bssource);
  315.     register short i;
  316.     register char ch = chdelim;
  317.     
  318.     for (i = len; i > 0; i--) {
  319.         
  320.         if (bssource [i] == ch) {
  321.             
  322.             midstring (bssource, i + 1, len - i, bsdest);
  323.             
  324.             return (true);
  325.             }
  326.         } /*for*/
  327.         
  328.     copystring (bssource, bsdest);
  329.     
  330.     return (true);
  331.     } /*lastword*/
  332.     
  333.     
  334. short patternmatch (bigstring bspattern, bigstring bs) {
  335.     
  336.     short lenstring = stringlength (bs);
  337.     short lenpattern = stringlength (bspattern);
  338.     short ixstring = 1;
  339.     byte chfirst;
  340.     short i, ix;
  341.     
  342.     if ((lenstring == 0) || (lenpattern == 0))
  343.         return (0);
  344.     
  345.     chfirst = bspattern [1];
  346.     
  347.     while (true) {
  348.         
  349.         if (bs [ixstring] == chfirst) { /*matched at least first character in string*/
  350.             
  351.             for (i = 2; i <= lenpattern; i++) {
  352.                 
  353.                 ix = ixstring + i - 1;
  354.                 
  355.                 if (ix > lenstring) /*gone off end of string, can't match*/
  356.                     return (0);
  357.                 
  358.                 if (bs [ix] != bspattern [i]) 
  359.                     goto L1;
  360.                 } /*for*/
  361.             
  362.             return (ixstring); /*loop terminated, full match*/
  363.             }
  364.         
  365.         L1: /*advance to next character in string*/
  366.         
  367.         if (++ixstring > lenstring) /*reached end of string, not found*/
  368.             return (0);
  369.         } /*while*/
  370.     } /*patternmatch*/
  371.  
  372.  
  373. void filledstring (char ch, short ct, bigstring bs) {
  374.     
  375.     if (ct < 0)
  376.         ct = 0;
  377.         
  378.     bs [0] = ct;
  379.     
  380.     fillchar (&bs [1], (long) ct, ch);
  381.     } /*filledstring*/
  382.     
  383.     
  384. boolean insertchar (char ch, bigstring bsdest) {
  385.     
  386.     register byte *pdest = bsdest;
  387.     register short len = stringlength (pdest);
  388.     
  389.     if (len == lenbigstring)
  390.         return (false);
  391.     
  392.     moveright (pdest + 1, pdest + 2, len);
  393.     
  394.     setstringlength (pdest, len + 1);
  395.     
  396.     pdest [1] = ch;
  397.     
  398.     return (true);
  399.     } /*insertchar*/
  400.  
  401.  
  402.